/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.util.HashMap;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.query.JRHibernateQueryExecuterFactory;
import net.sf.jasperreports.swing.JRViewer;
import org.hibernate.Session;
import util.HibernateUtil;

/**
 *
 * @author LaiHuynh
 */
public class SubReport extends javax.swing.JInternalFrame {

    /**
     * Creates new form SubReport
     */
    public SubReport() {
        super("Report Viewer", true, true, true, true);
        initComponents();
        setBounds(10, 10, 600, 500);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    public SubReport(String fileName) throws JRException {
        this(fileName, null);
    }

    public SubReport(String fileName, HashMap parameter) throws JRException {
        this();
        Session ss = HibernateUtil.getSessionFactory().openSession();
        parameter.put(JRHibernateQueryExecuterFactory.PARAMETER_HIBERNATE_SESSION, ss);
        JasperPrint print = JasperFillManager.fillReport(fileName, parameter);
        JRViewer viewer = new JRViewer(print);
        Container c = getContentPane();
        c.setLayout(new BorderLayout());
        c.add(viewer);
        ss.close();
//        this();
//        try {
//            Session session = HibernateUtil.getSessionFactory().openSession();
//            JasperPrint print = JasperFillManager.fillReport(fileName, parameter, session.connection());
//            JRViewer viewer = new JRViewer(print);
//            Container c = getContentPane();
//            c.setLayout(new BorderLayout());
//            c.add(viewer);
//        } catch (JRException jre) {
//            jre.printStackTrace();
//        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 394, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 274, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
